package com.orion.config;

import cn.hutool.json.JSONUtil;
import com.orion.domain.Result;
import com.orion.enumeration.Oauth2ExceptionCodeEnum;
import com.orion.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Administrator
 * @date 2021/10/11
 */
@Slf4j
@Component
public class CustomAuthenticationEntryPoint extends OAuth2AuthenticationEntryPoint{
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
            throws IOException, ServletException {
        log.info("resource authentication entry point : " , authException);
        Oauth2ExceptionCodeEnum unauthorized = Oauth2ExceptionCodeEnum.UNAUTHORIZED;
        Result result = new Result(unauthorized.getMsg(),unauthorized.getCode());
        ResponseUtil.jsonResp(response).getWriter().write(JSONUtil.toJsonStr(result));
    }
}
